package com.baize.imagehandledemo

import android.Manifest
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : AppCompatActivity(), ImageBottomSheetFragment.ImageResultListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //申请权限
        val permissions = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
        PermissionUtils.needsPermissions(this, permissions)
        iv_show.setOnClickListener {
            pichPhone()
        }
    }

    private fun pichPhone() {
        val bottomDialog = ImageBottomSheetFragment.getInstance(this, true)
        bottomDialog.show(supportFragmentManager, "image")
    }

    //图片返回
    override fun resultImagePath(path: String) {
        Toast.makeText(this, "图片路径：$path", Toast.LENGTH_SHORT).show()
        val bitmap = BitmapFactory.decodeFile(path)
        iv_show.setImageBitmap(bitmap)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            ImageBottomSheetFragment.REQUEST_CROP_113 -> {//剪裁返回
                if (resultCode == RESULT_OK) {
                    val path = ImageToolUtils.getCropdFilePath(this@MainActivity).absolutePath
                    resultImagePath(path)
                }
            }
        }
    }

}
